<?php

if (!function_exists('uuid_generate')) {
    /**
     * 生成唯一不重复的uuid
     * @return string
     */
    function uuid_generate()
    {
        return md5(\Illuminate\Support\Facades\Redis::incr('uuid_generate_id') . uniqid(mt_rand(), true));
    }
}

if (!function_exists('create_num')) {
    /**
     * 生成编号
     * @param string $type kh:客户，ht:合同,dd:订单
     * @return string
     */
    function create_num(string $type = 'kh')
    {
        return strtoupper($type).date('Ymd').str_pad(\Illuminate\Support\Facades\Redis::incr('generate_num_id'),5,"0",STR_PAD_LEFT);
    }
}

if (!function_exists('recursive')) {
    /**
     * 递归树形上下级
     * @param $data
     * @param $pid
     * @return array
     */
    function recursive($data, $pid = 0)
    {
        $re_data = [];
        foreach ($data as $d) {
            if ($d->parent_id == $pid) {
                $re_data[$d->id] = $d;
                $re_data[$d->id]['children'] = recursive($data, $d->id);
            } else {
                continue;
            }
        }
        return array_values($re_data);
    }
}

if (!function_exists('push_message')) {
    /**
     * @param $scene
     * @param $data ["title"=>"xxx","content"=>"xxxxx"]
     * @param array $accept_user_ids
     * @param int $send_user_id
     * @return bool
     * @throws GuzzleException
     */
    function push_message($scene,$data, $accept_user_ids = [], $send_user_id = 0)
    {
        try {
            \Illuminate\Support\Facades\Redis::rpush(config('freeswitch.redis_key.send_msg'),json_encode([
                'scene' => $scene,
                'data' => $data,
                'accept_user_ids' => $accept_user_ids,
                'send_user_id' => $send_user_id,
            ]));
            return true;
        } catch (GuzzleException $exception) {
            \Illuminate\Support\Facades\Log::error('推送消息异常：' . $exception->getMessage());
            return false;
        }
    }
}



